﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SourceCode.K2ROM;
using Lisphi.Lab.K2Site.Properties;
using Lisphi.Lab.K2Lib;

namespace Lisphi.Lab.K2Site
{
    public partial class _Default : System.Web.UI.Page
    {
        public int ProcInstID 
        {
            get { return this.ViewState["ProcInstID"] == null ? 0 : (int)this.ViewState["ProcInstID"]; }
            set { this.ViewState["ProcInstID"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtFolio.Text = DateTime.Now.ToString("yyyyMMddHHmmss");
            }
        }

        protected void btnCreateProcess_Click(object sender, EventArgs e)
        {
            K2Library k2Lib = this.GetK2Library();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("AuditUser", "chunleih");
            parameters.Add("LineValue", "0102");
            ProcessInstance procInst = k2Lib.StartProcess(this.txtFolio.Text, parameters);
            this.StoreProcInst(procInst);
        }

        protected void btnOpenWorklist_Click(object sender, EventArgs e)
        {
            K2Library k2Lib = this.GetK2Library();
            int procInstID = 0;
            int.TryParse(this.txtProcInstID.Text, out procInstID);
            Worklist worklist = k2Lib.OpenWorklist(procInstID);
            this.grdWorklist.DataSource = worklist;
            this.grdWorklist.DataBind();
            this.lblRowCount.Text = worklist.Count.ToString();
            if (worklist.Count > 0)
            {
                this.StoreProcInst(worklist[0]);
            }
        }

        protected void btnMoveWorklistItem_Click(object sender, EventArgs e)
        {
            K2Library k2Lib = this.GetK2Library();
            string serialNumber = this.GetSerialNumber();
            Dictionary<string, string> parameters = this.GetMoveParameters();
            k2Lib.MoveWorklistItem(serialNumber, parameters);
            this.btnOpenWorklist_Click(null, null);
        }

        protected void btnGotoActivity_Click(object sender, EventArgs e)
        {
            K2Library k2Lib = this.GetK2Library();
            string serialNumber = this.GetSerialNumber();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("AuditUser", txtGotoAuditUser.Text);
            k2Lib.GotoActivity(serialNumber, this.txtGotoActivity.Text, parameters);
            this.btnOpenWorklist_Click(null, null);
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            this.Response.Redirect("Login.aspx");
        }

        private string GetSerialNumber()
        {
            string serialNumber = string.Empty;
            K2Library k2Lib = this.GetK2Library();
            int procInstID = 0;
            int.TryParse(this.txtProcInstID.Text, out procInstID);
            Worklist worklist = k2Lib.OpenWorklist(procInstID);
            if (worklist != null && worklist.Count > 0)
            {
                serialNumber = worklist[0].SerialNumber;
            }
            return serialNumber;
        }

        private K2Library GetK2Library()
        {
            UserInfo user = UserInfoList.GetUser(this.User.Identity.Name);
            if (user == null) return null;
            string k2ConnStr = string.Format(Settings.Default.K2ConnStr, Settings.Default.K2Domain, user.UserName, user.Password);
            K2Library k2Lib = new K2Library(Settings.Default.K2Server, k2ConnStr);
            k2Lib.ProcessName = "eWorkPlaceII\\eTest";
            return k2Lib;
        }

        private K2Man GetK2Man()
        {
            K2Man k2Man = new K2Man();
            k2Man.AdminName = Settings.Default.K2AdminName;
            k2Man.AdminPassword = Settings.Default.K2AdminPassword;
            k2Man.DomainName = Settings.Default.K2Domain;
            k2Man.Port = Settings.Default.K2Port;
            k2Man.Server = Settings.Default.K2Server;
            return k2Man;
        }

        private Dictionary<string, string> GetMoveParameters()
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("AuditUser", this.txtAuditUser.Text);
            parameters.Add("LineValue", this.txtLineValue.Text);
            return parameters;
        }

        private void StoreProcInst(ProcessInstance procInst)
        {
            if (procInst == null)
            {
                this.ProcInstID = 0;
            }
            else
            {
                this.ProcInstID = procInst.ID;
            }
            this.txtProcInstID.Text = this.ProcInstID.ToString();
        }

        private void StoreProcInst(WorklistItem worklistItem)
        {
            if (worklistItem == null)
            {
                this.ProcInstID = 0;
                this.txtProcInstID.Text = this.ProcInstID.ToString();
            }
            else
            {
                this.StoreProcInst(worklistItem.ProcessInstance);
            }
        }

        protected void btnGetWorklistFromk2Mng_Click(object sender, EventArgs e)
        {
            K2Man k2Man = this.GetK2Man();
            SourceCode.K2Mng.WorkListItems workListItems = k2Man.GetWorkListItems("eWorkPlaceII\\eTest", txtFolioForK2Mng.Text);
            grdWorklistFromk2Mng.DataSource = workListItems;
            grdWorklistFromk2Mng.DataBind();
        }

        protected void btnRedirect_Click(object sender, EventArgs e)
        {
            K2Man k2Man = this.GetK2Man();
            SourceCode.K2Mng.WorkListItems workListItems = k2Man.GetWorkListItems("eWorkPlaceII\\eTest", txtFolioForK2Mng.Text);
            foreach (SourceCode.K2Mng.WorkListItem workListItem in workListItems)
            {
                k2Man.RedirectWorklistItem((int)workListItem.ID, txtUserName.Text);
            }
        }
    }
}
